package com.suns.controller;

import com.suns.commom.ApplicationContextHelper;
import com.suns.commom.JsonData;
import com.suns.dao.SysAclModuleMapper;
import com.suns.exception.ParamException;
import com.suns.exception.PermissionException;
import com.suns.model.SysAclModule;
import com.suns.param.TestVo;
import com.suns.util.BeanValidator;
import com.suns.util.JsonMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/test")
@Slf4j
public class TestController {
    @RequestMapping("/validate.json")
    @ResponseBody
    public JsonData validate(TestVo vo) throws ParamException{
        log.info("validate");

        //测试ApplicationContextHelper
        SysAclModuleMapper moduleMapper= ApplicationContextHelper.popBean(SysAclModuleMapper.class);
        SysAclModule module=moduleMapper.selectByPrimaryKey(1);

        //测试JsonMapper
        log.info(JsonMapper.obj2String(module));
        BeanValidator.check(vo);

        return JsonData.success("test validate");
    }

    /*
        @RequestMapping("/validate.json")
        @ResponseBody
        public JsonData validate(TestVo vo) throws ParamException{
            log.info("validate");

            //使用ParamException
            BeanValidator.check(vo);

            return JsonData.success("test validate");
        }
    */

    @RequestMapping("/hello.json")
    @ResponseBody
    public JsonData hello() {
        log.info("hello");
        //throw new RuntimeException("test exception");
        //throw new PermissionException("test exception");
        return JsonData.success("hello,permission");
    }

   /*
   @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        log.info("hello");
        return "hello,permission";
    }
    */
}
